/*********************************************************************************
 *      Copyright:  (C) 2025 iot25<lingyun@email.com>
 *                  All rights reserved.
 *
 *       Filename:  judge_connect.c
 *    Description:  This file judge_connect.c
 *                 
 *        Version:  1.0.0(01/05/25)
 *         Author:  iot25 <lingyun@email.com>
 *      ChangeLog:  1, Release initial version on "01/05/25 08:56:22"
 *                 
 ********************************************************************************/
#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/tcp.h>
#include<netinet/in.h>
#include<string.h>
#include<errno.h>
#include <unistd.h>

#include"judge_connect.h"
#include"logger.h"
#include"client_connect_sever.h"

int judge_connect(socket_t *sock)
{
	struct tcp_info 			info;
	socklen_t					len = sizeof(info);

	if( !sock )
	{
		log_error("Invalid argument!\n");
		return -1;
	}
	if(sock->fd < 0)
	{
		return -2;//表示socket就没有创建成功
	}
	
	if(getsockopt(sock->fd, IPPROTO_TCP, TCP_INFO, &info, &len) == 0)
	{
		if(info.tcpi_state  == TCP_ESTABLISHED)
		{
			return 1;//代表连接正常
		}
		else
		{
			return -3;//连接失败
		}
	}
	else
	{
		log_error("Getsockopt failuer: %s\n", strerror(errno));
		return -4;//获取连接状态失败
	}

}

